#include "testpage.h"
#include "ui_testpage.h"
#include "serialportdriver.h"
#include "serialportprotocol.h"
#include "netdriver.h"
#include "netprotocol.h"
#include <QMessageBox>
#include <QDebug>
#include <QColorDialog>
#include <QTimer>
#include <QJsonObject>
#include <QJsonDocument>
#include "datatemp.h"

TestPage::TestPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TestPage)
  ,requestAllTimer(new QTimer)//请求的定时器
  ,upLoadTimer(new QTimer)//上报的定时器
{
    ui->setupUi(this);
    ui->leDeviceId->setText(DataTemp::getObject()->deviceId);
    ui->leDeviceSecretKey->setText(DataTemp::getObject()->deviceKey);
    ui->cbSerialPortName ->addItem(
        DataTemp::getObject()->serialName);
    ui->cbBaudRate->addItem(
        QString("%1").arg(DataTemp::getObject()->serialBaudRate)
        );
    ui->leSendTime->setText(QString("%1").arg(DataTemp::getObject()->uploadTime));
    ui->leReqTime->setText(QString("%1").arg(DataTemp::getObject()->reqTime));
    qDebug() << "serialbaudrate" <<DataTemp::getObject()->serialBaudRate;
    ui->leTcpAddr->setText(DataTemp::getObject()->netIp);
    ui->leTcpPort->setText(QString("%1").arg(DataTemp::getObject()->netPort));
     ui->btnCloseSerialPort->setEnabled(false);
     ui->btnClose->setEnabled(false);
    if(DataTemp::getObject()->mode == 1){
        ui->btnModeChange->setText("Auto");
      on_btnOpenSerialPort_clicked();
      on_btnConnect_clicked();

      requestAllTimer->start(DataTemp::getObject()->reqTime * 1000);
      upLoadTimer->start(DataTemp::getObject()->uploadTime * 1000);

    }
    QStringList list=SerialPortDriver::getObject()->serialPortGetAveList();
    ui->cbSerialPortName->addItems(list);

     ui->cbBaudRate->addItem("9600");
    ui->cbBaudRate->addItem("19200");
    ui->cbBaudRate->addItem("38400");
    //ui->cbBaudRate->addItem("57600");
    ui->cbBaudRate->addItem("115200");


    connect(SerialPortProtocol::getObject(),&SerialPortProtocol::showRequestMessage,this,&TestPage::showRequestMessageSlot);
    connect(NetProtocol::getObject(),&NetProtocol::showMessage,this,&TestPage::showLoginMessageSlot);
    connect(requestAllTimer,&QTimer::timeout,this,&TestPage::autoRequsetAllSlot);
    connect(upLoadTimer,&QTimer::timeout,this,&TestPage::upLoadSlot);
    connect(SerialPortProtocol::getObject(),&SerialPortProtocol::showopenStatus,this,&TestPage::openStatusSlot);
}

TestPage::~TestPage()
{
    delete ui;
}



void TestPage::on_btnOpenSerialPort_clicked()
{
bool ok = SerialPortDriver::getObject()->serialPortOpen(
            ui->cbSerialPortName->currentText(),
            ui->cbBaudRate->currentText().toInt());//打开串口传入端口号和波特率
if(ok)
{
    ui->cbBaudRate->setEnabled(false);
    ui->cbSerialPortName->setEnabled(false);
    ui->btnOpenSerialPort->setEnabled(false);
    ui->btnCloseSerialPort->setEnabled(true);
    }
    else if(!ok)
    {
        QMessageBox::warning(this,"warning","打开失败！");
        return;
    }
    ui->tbShowMessage->setPlainText(ok ?"ok" : "error");


    }

    void TestPage::on_btnCloseSerialPort_clicked()
    {
        SerialPortDriver::getObject()->serialPortClose();

    ui->cbBaudRate->setEnabled(true);
    ui->cbSerialPortName->setEnabled(true);
    ui->btnOpenSerialPort->setEnabled(true);
    ui->btnCloseSerialPort->setEnabled(false);
    }

void TestPage::showRequestMessageSlot(const QString &msg,const int &devicenum)
{
ui->tbShowMessage->append(msg);
switch (devicenum)
{



}
}

void TestPage::showLoginMessageSlot(const QString &message)
{
ui->tbShowMessage->append(message);
}



void TestPage::autoRequsetAllSlot()
{

SerialPortProtocol::getObject()->requestTem(1);
SerialPortProtocol::getObject()->requestTem(2);
SerialPortProtocol::getObject()->requestHum1(1);
SerialPortProtocol::getObject()->requestHum1(2);
SerialPortProtocol::getObject()->requestLight(1);
SerialPortProtocol::getObject()->requestLight(2);
SerialPortProtocol::getObject()->requestPress(1);
SerialPortProtocol::getObject()->requestPress(2);
SerialPortProtocol::getObject()->requestHum2(1);
SerialPortProtocol::getObject()->requestHum2(2);
}


void TestPage::on_cbAutoRequest_stateChanged(int arg1)
{

if(arg1==2)
    {
    requestAllTimer->start(ui->leReqTime->text().toInt()*1000);
    }
else {
    requestAllTimer->stop();
     }

}

void TestPage::on_btnConnect_clicked()
{
    if(ui->cbLogin->isChecked()){
bool ok=NetDriver::getObject()->netConnect1(ui->leTcpAddr->text(),ui->leTcpPort->text().toInt());
if(ok)
      {
bool re=NetProtocol::getObject()->deviceLogin(ui->leDeviceId->text(),ui->leDeviceSecretKey->text());
if(!re)
    QMessageBox::information(this,"info","send error");
      }
    }
    else if(!ui->cbLogin->isChecked())
    {
     NetDriver::getObject()->netConnect2(ui->leTcpAddr->text(),ui->leTcpPort->text().toInt());
    }
    ui->btnClose->setEnabled(true);
    ui->btnConnect->setEnabled(false);
}

void TestPage::on_btnClose_clicked()
{
    NetDriver::getObject()->netDisconnect();
 ui->btnConnect->setEnabled(true);
 ui->btnClose->setEnabled(false);
}

void TestPage::on_btnAutoSend_clicked()
{
if(ui->btnAutoSend->text()=="自动发送"){
    upLoadTimer->start(ui->leSendTime->text().toInt()*1000);
    qDebug() << ui->leSendTime->text().toDouble();
    ui->btnAutoSend->setText("停止");
 }
else if(ui->btnAutoSend->text()==
        "停止")
     {
      upLoadTimer->stop();
      ui->btnAutoSend->setText("自动发送");
     }
}

void TestPage::upLoadSlot()
{
    if(currentBoardFlag == 0)
    {
    double b1_tem = DataTemp::getObject()->b1_tem;
    double b1_hum1 = DataTemp::getObject()->b1_hum1;
    double b1_light = DataTemp::getObject()->b1_light;
    double b1_press = DataTemp::getObject()->b1_press;
    double b1_hum2 = DataTemp::getObject()->b1_hum2;
    bool b1_water1 = DataTemp::getObject()->b1_water1;
    bool b1_water2 = DataTemp::getObject()->b1_water2;
    NetProtocol::getObject()->uploadSensorData(1,b1_tem,b1_hum1,b1_hum2,b1_light,b1_press,b1_water1,b1_water2);   //(int boardId, double tem, double hum1, double hum2,
       currentBoardFlag = 1;
       return;
    }
    else if(currentBoardFlag == 1)
    {
       double b2_tem = DataTemp::getObject()->b2_tem;
       double b2_hum1 = DataTemp::getObject()->b2_hum1;
       double b2_light = DataTemp::getObject()->b2_light;
       double b2_press = DataTemp::getObject()->b2_press;
       double b2_hum2 = DataTemp::getObject()->b2_hum2;
       bool b2_water1 = DataTemp::getObject()->b2_water1;
       bool b2_water2 = DataTemp::getObject()->b2_water2;
       NetProtocol::getObject()->uploadSensorData(2,b2_tem,b2_hum1,b2_hum2,b2_light,b2_press,b2_water1,b2_water2);
       currentBoardFlag = 0;
          return;
    }


}

void TestPage::on_btnUpLoadData_clicked()
{
    double b1_tem = DataTemp::getObject()->b1_tem;
    double b1_hum1 = DataTemp::getObject()->b1_hum1;
    double b1_light = DataTemp::getObject()->b1_light;
    double b1_press = DataTemp::getObject()->b1_press;
    double b1_hum2 = DataTemp::getObject()->b1_hum2;
    bool b1_water1 = DataTemp::getObject()->b1_water1;
    bool b1_water2 = DataTemp::getObject()->b1_water2;
    NetProtocol::getObject()->uploadSensorData(1,b1_tem,b1_hum1,b1_hum2,b1_light,b1_press,b1_water1,b1_water2);
    double b2_tem = DataTemp::getObject()->b2_tem;
    double b2_hum1 = DataTemp::getObject()->b2_hum1;
    double b2_light = DataTemp::getObject()->b2_light;
    double b2_press = DataTemp::getObject()->b2_press;
    double b2_hum2 = DataTemp::getObject()->b2_hum2;
    bool b2_water1 = DataTemp::getObject()->b2_water1;
    bool b2_water2 = DataTemp::getObject()->b2_water2;
    NetProtocol::getObject()->uploadSensorData(2,b2_tem,b2_hum1,b2_hum2,b2_light,b2_press,b2_water1,b2_water2);

}

void TestPage::on_btnSaveUserMessage_clicked()
{
    DataTemp::getObject()->deviceId=ui->leDeviceId->text();//将变量存在数据模块里
    DataTemp::getObject()->deviceKey = ui->leDeviceSecretKey->text();
}

void TestPage::on_btnSaveSendTime_clicked()
{
   DataTemp::getObject()->uploadTime=ui->leSendTime->text().toInt();
}

void TestPage::on_btnModeChange_clicked()
{
    if(ui->btnModeChange->text() == "Manual")
    {//auto
        DataTemp::getObject()->mode = 1;
        requestAllTimer->start(DataTemp::getObject()->reqTime * 1000);
        upLoadTimer->start(DataTemp::getObject()->uploadTime * 1000);
        ui->btnModeChange->setText("Auto");
    }
    else
    {
        DataTemp::getObject()->mode = 0;
        requestAllTimer->stop();
        upLoadTimer->stop();
        ui->btnModeChange->setText("Manual");
    }
}

void TestPage::on_btnSaveReqTime_clicked()
{
    DataTemp::getObject()->reqTime=ui->leReqTime->text().toInt();
}

void TestPage::requestDataSlot()
{QPushButton *btn = qobject_cast<QPushButton *>(sender());
    if( btn->objectName() == "btnB1tem")
        SerialPortProtocol::getObject()->requestTem(1);

   else if( btn->objectName() == "btnB1hum1")
        SerialPortProtocol::getObject()->requestHum1(1);

    else if( btn->objectName() == "btnB1light")
         SerialPortProtocol::getObject()->requestLight(1);

    else if( btn->objectName() == "btnB1press")
         SerialPortProtocol::getObject()->requestPress(1);

    else if( btn->objectName() == "btnB1hum2")
         SerialPortProtocol::getObject()->requestHum2(1);

    else if( btn->objectName() == "btnB2tem")
         SerialPortProtocol::getObject()->requestTem(2);

    else if( btn->objectName() == "btnB2hum1")
         SerialPortProtocol::getObject()->requestHum1(2);

    else if( btn->objectName() == "btnB2light")
         SerialPortProtocol::getObject()->requestLight(2);

    else if( btn->objectName() == "btnB2press")
         SerialPortProtocol::getObject()->requestPress(2);

    else if( btn->objectName() == "btnB2hum2")
         SerialPortProtocol::getObject()->requestHum2(2);


}

void TestPage::deviceControlSlot()
{
    QPushButton *btn = qobject_cast<QPushButton *>(sender());
if(btn->objectName() == "btnB1water1" && btn->text() =="已关闭")
    SerialPortProtocol::getObject()->openWater1(1);
else if(btn->objectName() == "btnB1water2" && btn->text() =="已关闭")
    SerialPortProtocol::getObject()->openWater2(1);
else if(btn->objectName() == "btnB2water1" && btn->text() =="已关闭")
    SerialPortProtocol::getObject()->openWater1(2);
else if(btn->objectName() == "btnB2water2" && btn->text() =="已关闭")
    SerialPortProtocol::getObject()->openWater2(2);
if(btn->objectName() == "btnB1water1" && btn->text() =="已打开")
    SerialPortProtocol::getObject()->closeWater1(1);
else if(btn->objectName() == "btnB1water2" && btn->text() =="已打开")
    SerialPortProtocol::getObject()->closeWater2(1);
else if(btn->objectName() == "btnB2water1" && btn->text() =="已打开")
    SerialPortProtocol::getObject()->closeWater1(2);
else if(btn->objectName() == "btnB2water2" && btn->text() =="已打开")
    SerialPortProtocol::getObject()->closeWater2(2);



}

void TestPage::openStatusSlot(int boardId, int deviceId, int status)
{
if(boardId == 0x10)
{
   if (deviceId ==0x06)
   {
       if(status == 0x00)
           ui->btnB1water1->setText("已关闭");
       else
           ui->btnB1water1->setText("已打开");
   }
  else if (deviceId ==0x07)
   {
       if(status == 0x00)
           ui->btnB1water2->setText("已关闭");
       else
           ui->btnB1water2->setText("已打开");
   }


}
else {
    if (deviceId ==0x06)
    {
        if(status == 0x00)
            ui->btnB2water1->setText("已关闭");
        else
            ui->btnB2water1->setText("已打开");
    }
   else if (deviceId ==0x07)
    {
        if(status == 0x00)
            ui->btnB2water2->setText("已关闭");
        else
            ui->btnB2water2->setText("已打开");
    }
}
}
